<template>
  <div class="container">
    <div class="button-row">
      <el-button @click="addTbItems()">添加表元素</el-button>
      <el-button @click="clsTbItems()" type="primary">清空表元素</el-button>
      <el-button @click="handleButtonClick('参数')" type="success">消息提醒</el-button>
    </div>

    <div class="table-section">
      <el-table
          :data="dataList"
          border style="width: 100%" :header-cell-style="{ background: '#f5f7fa' }">
        <el-table-column
            prop="name"
            label="Name"
            minWidth="120"
            align="center"
            header-align="center">
        </el-table-column>
        <el-table-column
            prop="age"
            label="Age"
            min-width="120"
            align="center"
            header-align="center">
        </el-table-column>
        <el-table-column
            prop="email"
            label="Email"
            align="center"
            header-align="center">
        </el-table-column>
      </el-table>
    </div>
  </div>
</template>

<script lang="ts" setup>
import {ref} from "vue";
import {ElMessage} from "element-plus";


const dataList = ref([
  {
    name: "Tom",
    age: 26,
    email: "tom@example.com"
  },
  {
    name: "Bob",
    age: 23,
  },
]);

const handleButtonClick = (type: string) => {
  ElMessage.info(`点击发送的是： ${type} `);
};
const flag = ref(false)
const clsTbItems = () => {
  dataList.value = [];
}
const addTbItems = () => {
  if (!flag.value) {
    dataList.value = []
    flag.value = true;
  }
  dataList.value.push({
    name: "name_" + randomInt(1, 30),
    age: randomInt(30, 90),
    email: randomInt(100, 300) + "@example.com"
  })
}
const randomInt = (min: number, max: number) => {
  min = Math.ceil(min); // 向上取整，确保min为整数
  max = Math.floor(max); // 向下取整，确保max为整数
  if (min > max) throw new Error("max必须大于等于min");
  return Math.floor(Math.random() * (max - min + 1)) + min;
}


</script>

<style scoped>
.container {
  padding: 20px;
  font-family: 'Helvetica Neue', Arial, sans-serif;
}

.button-row {
  display: flex;
  flex-wrap: wrap;
  gap: 12px;
  align-items: center;
  margin-bottom: 24px;
}

.table-section {
  margin-top: 20px;
  border-radius: 4px;
  overflow: hidden;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
}

/* 确保表格单元格对齐 */
:deep(.el-table) {
  font-size: 14px;
}

:deep(.el-table th) {
  font-weight: 600;
  color: #303133;
}

:deep(.el-table .cell) {
  padding: 8px 12px;
  line-height: 1.5;
  word-break: break-word;
}

/* 解决表头与内容不对齐的问题 */
:deep(.el-table__header) {
  width: 100% !important;
}

:deep(.el-table__body) {
  width: 100% !important;
}

:deep(.el-table__header-wrapper) {
  width: 100% !important;
}

:deep(.el-table__body-wrapper) {
  width: 100% !important;
}

:deep(.el-table th > .cell) {
  white-space: nowrap;
}

:deep(.el-table .el-table__cell) {
  box-sizing: border-box;
}
</style>